layui.config({
    version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
    form = layui.form;
    layer = layui.layer;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
    inituserdata();
   $(".add_rulesbtn").click(function(){
        $("#rules_old").append(goldList);
    });
});

function inituserdata(){
    $.ajax({
        url: httpurl+"/SelectGoldcoinsetupList",
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        cache: false,
        success: function(data){
            // console.log(JSON.stringify(data)+"=======dataList");
            if(data.rtnCode==200){
                let dataList=data.data;
                var rule="";
                for(let i=0;i<dataList.length;i++){
                    rule+='<div class="rules margin_buttom10 display_table">' +
                                '<div class="float_left">' +
                                '<span class="margin_right10">天数</span><input type="text" value="'+dataList[i].days+'"  class="form-control form-boxed width20 days" /><span></span>' +
                                '<span class="margin_right10"> 获得金币数量</span><input type="number" value="'+dataList[i].number+'" step="1" class="form-control form-boxed width20 getgold" /><span></span>' +
                                '<span class="margin_right10"> 奖励金币数量</span><input type="number"value="'+dataList[i].reward+'"  step="1" class="form-control form-boxed width20 rewgold" /><span></span>' +
                                '</div>' +
                                '<div class="float_left padding6 bg_red" onclick="icon_remove(this)"><i class="icon-remove"></i></div>' +
                            '</div>';

                }
                $(".AddgoldList").after(rule);
            }

        }
    });
}

function SubmitInfo() {
    let setmealInfo="[";
    $(".rules").each(function () {
        let days=$(this).find(".days").val();
        let getgold=$(this).find(".getgold").val();
        let rewgold=$(this).find(".rewgold").val();

        if(days==""){
            top.layer.alert("存在未填写的天数!");
            return;
        }
        if(getgold==""){
            top.layer.alert("存在未填写的获得金币数量!");
            return;
        }
        if(rewgold==""){
            top.layer.alert("存在未填写的奖励金币数量!");
            return;
        }
        var list='{"days":"'+days+'","number":"'+getgold+'","reward":"'+rewgold+'"}'+",";
        setmealInfo+=list;
    })
    setmealInfo=setmealInfo.substring(0,setmealInfo.length-1);
    setmealInfo+="]";

    if(setmealInfo.length<=0){
        top.layer.alert("请至少填写一个金币规则!");
        return;
    }
    top.layer.confirm('确认提交？',{icon: 3,title:'提示信息'},function(index){
        var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: httpurl+"/InsertGoldcoinsetup",
            cache: false,
            data: {
                "list":setmealInfo,
            },
            dataType: "json",
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "*",
                "Access-Control-Allow-Headers": "Authorization",
                "Authorization": GetTokenInfo()
            },
            contentType : 'application/json',
            success: function(data) {
                layer.closeAll();
                top.layer.msg(data.msg);
                if(data.rtnCode==200){
                    window.location.href="GoldList.html";
                }
            }
        });
    },function(){

    });
}
